import paddle
"""
Auto-batch utils
"""
from copy import deepcopy
import numpy as np
from utils.general import colorstr
from utils.torch_utils import profile


def check_train_batch_size(model, imgsz=640):
    with paddle.amp.auto_cast():
        return autobatch(deepcopy(model).train(), imgsz)


def autobatch(model, imgsz=640, fraction=0.9, batch_size=16):
    prefix = colorstr('autobatch: ')
    print(f'{prefix}Computing optimal batch size for --imgsz {imgsz}')
    device = next(model.parameters()).place
    if device.type == 'cpu':
        print(
            f'{prefix}CUDA not detected, using default CPU batch-size {batch_size}'
            )
        return batch_size
    d = str(device).upper()
    t = paddle.device.cuda.get_device_properties(device=device
        ).total_memory / 1024 ** 3
    r = paddle.device.cuda.memory_reserved(device=device) / 1024 ** 3
    a = paddle.device.cuda.memory_allocated(device=device) / 1024 ** 3
    f = t - (r + a)
    print(
        f'{prefix}{d} {t:.3g}G total, {r:.3g}G reserved, {a:.3g}G allocated, {f:.3g}G free'
        )
    batch_sizes = [1, 2, 4, 8, 16]
    try:
        img = [paddle.zeros(shape=[b, 3, imgsz, imgsz]) for b in batch_sizes]
        y = profile(img, model, n=3, device=device)
    except Exception as e:
        print(f'{prefix}{e}')
    y = [x[2] for x in y if x]
    batch_sizes = batch_sizes[:len(y)]
    p = np.polyfit(batch_sizes, y, deg=1)
    b = int((f * fraction - p[1]) / p[0])
    print(
        f'{prefix}Using colorstr(batch-size {b}) for {d} {t * fraction:.3g}G/{t:.3g}G ({fraction * 100:.0f}%)'
        )
    return b
